<template>
  <div>
    <van-nav-bar title="标题" left-arrow @click-left="$router.back()" />

    <input
      type="file"
      hidden
      ref="input"
      multiple
      accept=".png,.jpg"
      @change="onSlected"
    />
    <van-cell is-link title="头像" @click="$refs.input.click()">
      <van-image round width="0.8rem" height="0.8rem" :src="userInfo.photo" />
    </van-cell>
    <van-cell is-link title="昵称" @click="isShowNcName = true">
      <span>{{ userInfo.name }}</span>
    </van-cell>
    <van-cell is-link title="性别" @click="isShowGender = true">
      <span>{{ userInfo.gender === 0 ? '男' : '女' }}</span>
    </van-cell>
    <van-cell is-link title="生日" @click="isShowBirthday = true">
      <span>{{ userInfo.birthday }}</span>
    </van-cell>

    <van-popup
      v-model="isShowAvatar"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-avater
        :photo="photo"
        @close="isShowAvatar = false"
        v-if="isShowAvatar"
        @update-photo=";(userInfo.photo = $event), (isShowAvatar = false)"
      ></update-avater>
    </van-popup>

    <van-popup
      v-model="isShowNcName"
      position="bottom"
      :style="{ height: '100%' }"
    >
      <update-nick-name
        v-if="userInfo.name && isShowNcName"
        :name.sync="userInfo.name"
        @close="isShowNcName = false"
      ></update-nick-name>
    </van-popup>

    <van-popup
      v-model="isShowGender"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <update-gerder
        v-if="userInfo.name && isShowGender"
        :gender.sync="userInfo.gender"
        @close="isShowGender = false"
      ></update-gerder>
    </van-popup>

    <van-popup
      v-model="isShowBirthday"
      position="bottom"
      :style="{ height: '30%' }"
    >
      <update-brithday
        v-if="userInfo.birthday && isShowBirthday"
        :birthday.sync="userInfo.birthday"
        @close="isShowBirthday = false"
      ></update-brithday>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfileAPI } from '@/api'
import UpdateNickName from './components/UpdateNickName.vue'
import UpdateGerder from './components/UpdateGerder.vue'
import UpdateBrithday from './components/UpdateBrithday.vue'
import UpdateAvater from './components/UpdateAvater.vue'
import { resolveToBase64 } from '@/utils'
export default {
  components: {
    UpdateNickName,
    UpdateGerder,
    UpdateBrithday,
    UpdateAvater
  },
  data() {
    return {
      userInfo: [],
      isShowNcName: false,
      isShowGender: false,
      isShowBirthday: false,
      isShowAvatar: false,
      photo: ''
    }
  },
  created() {
    this.getUserProfile()
  },
  methods: {
    async getUserProfile() {
      const { data } = await getUserProfileAPI()
      this.userInfo = data.data
    },
    async onSlected() {
      // 1 获取用户选择的的文件 保存在input.file
      const file = this.$refs.input.files[0]
      // console.log(file)

      // 2 将file对象转成base64
      const base64 = await resolveToBase64(file)
      // console.log(base64)
      this.photo = base64

      // 3 打开弹出层
      this.isShowAvatar = true

      // 4 手动将input.value属性清空
      this.$refs.input.value = ''
    }
  }
}
</script>

<style></style>
